package com.example.demo.controller;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.entity.WiselyMessage;
import com.example.entity.WiselyResponse;

@Controller
public class WsController {
	@Autowired
	private SimpMessagingTemplate messagingTemplate;

	@MessageMapping("/welcome")
	@SendTo("/topic/getReponse")
	public WiselyResponse say(WiselyMessage message) throws Exception {
		return new WiselyResponse("welcome, " + message.getName() + "!");
	}

	@RequestMapping("/ws")
	public String showVie() {
		return "ws";
	}
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
	@RequestMapping("/chat")
	public String chat() {
		return "chat";
	}

	@RequestMapping("/ws2")
	public String showVie2() {
		return "ws2";
	}

	@MessageMapping("/chat")
	public void handleChat(Principal principal, String message) {
		if (principal.getName().equals("xupx")) {
			messagingTemplate.convertAndSendToUser("xumh", "/queue/notifications",
					principal.getName() + "-send: " + message);
		}else {
			messagingTemplate.convertAndSendToUser("xupx", "/queue/notifications", principal.getName() + "-send: " + message);
		}
	}
}
